﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business;
using NHibernate;
using NHibernate.Criterion;
using ZLCMS.Business.DwzFrame;

namespace ZLCMS.Web.SystemManage.Basics.MemberCard
{
    public partial class List : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        protected void Page_Load(object sender, EventArgs e)
        {
            _framePage = new FrameHelper(this) { RecordCount = GetMemberCardCount() };

            if (_framePage.IsAjaxPagerPostBack)
            {
                BindGridMemberCard(_framePage.PageIndex, _framePage.PageSize);
            }
            else if (!IsPostBack)
            {
                BindGridMemberCard(1, 20);
            }
        }

        protected void gridMemberCard_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("target", "uid");
                e.Row.Attributes.Add("rel", e.Row.Cells[0].Text);

                e.Row.Cells[6].Text =
                    "<a class=\"edit\" href=\"/SystemManage/Basics/Member/Edit.aspx?uid=" + e.Row.Cells[0].Text + "\" height=\"550\" target=\"dialog\" mask=\"true\" title=\"操作员修改\">修改</a>";
                e.Row.Cells[7].Text =
                    "<a class=\"delete\" href=\"/SystemManage/Basics/Member/Delete.aspx?uid=" + e.Row.Cells[0].Text + "\" height=\"550\" target=\"ajaxTodo\" title=\"确定要删除吗?\">删除</a>";
            }
        }


        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小</param>
        protected void BindGridMemberCard(int pageIndex, int pageSize)
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria = session.CreateCriteria<ZLCMS.Data.Model.MemberCard>();
                criteria.SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize);

                gridMemberCard.DataSource = criteria.List();
                gridMemberCard.DataBind();
            }
        }

        protected int GetMemberCardCount()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria = session.CreateCriteria<ZLCMS.Data.Model.MemberCard>();
                return (int)criteria.SetProjection(Projections.RowCount()).UniqueResult();
            }
        }
    }
}
